package core;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;

import core.storage.XMLSerializer;

/**
 * ScoreBoard class<BR />
 * This class defines the ScoreBoard that contains
 * all the informations about the best score in
 * games played on a computer. It contains the player
 * who has established the score and his score and
 * reaction time.<BR />
 * This class' data is contained in a serialized file
 * that is imported each time the game is launched.
 * 
 * @version 1.0.0
 * @author Y. Bondue
 */
public final class ScoreBoard {

	private Vector<Score> scores;

	/** Necessary self reference for singleton implementation. */
    private static volatile ScoreBoard selfReference = null;

    /**
     * Private constructor necessary for a singleton implementation.
     */
    private ScoreBoard() {
    	scores = new Vector<Score>(10);
    }

    public static ScoreBoard getInstance() {
    	if (selfReference == null) {
    		selfReference = new ScoreBoard();
    	}
    	return selfReference;
    }
    
    public Vector<Score> getScores() {
		return selfReference.scores;
	}

	/**
     * suggestScore method<BR />
     * Score suggestion method. This method checks
     * if a given score is worthy of being introduced to
     * the score board.<BR />
     * If a score is good enough, it will take its
     * rightful place among the other scores.
     * 
     * @param score The score which will be determined as worthy or not.
     */
    public void suggestScore(Score score) {
    	if (score == null) {
    		// A null score is encountered each time a
    		// virtual player tries to suggest his score.
    		return;
    	}
    	if (scores.isEmpty()) {
    		scores.add(score);
    	} else {
	    	for (Score currentScore : scores) {
	    		if (currentScore.getAverageReactionTime() < score.getAverageReactionTime()) {
	    			scores.insertElementAt(score, scores.lastIndexOf(currentScore));
	    			serialize();
	    			break;
	    		}
	    	}
    	}
    }
    
    /**
     * serialize method<BR />
     * ScoreBoard serialization method. This method is called when
     * the ScoreBoard has to be serialized, which happens on 
     */
    public void serialize() {
    	try {
			XMLSerializer.store(selfReference, "scores");
		} catch (FileNotFoundException e) {
			System.out.println("Specified file is incorrect.");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("An I/O error has occured while serializing the score board.");
			e.printStackTrace();
		}
    }
    
    /**
     * parse method<BR />
     * ScoreBoard parsing method. This method is called when the
     * ScoreBoard has to be parsed, which happens on the game
     * launch.
     */
    public void parse() {
    	try {
			selfReference = ((ScoreBoard)XMLSerializer.load("scores"));
    	} catch (FileNotFoundException e) {
			System.out.println("Specified file is incorrect.");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("An I/O error has occured while parsing the score board.");
			e.printStackTrace();
		}
    }
}
